<?php
namespace system\service;

use system\service\interfaces\IBankAreaService;
use common\service\CommonService;
use herosphp\core\Loader;

Loader::import('system.service.interfaces.IBankAreaService');

/**
 * system 银行地区(Service)接口实现
 * @package system\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class BankAreaService extends CommonService implements IBankAreaService {
    /**
     * 生成JS文件
     * @return mixed
     */
    public function createJsFile()
    {

        $province = $this->getModelDao()->field('province')->group('province')->find();

        $p = ''; //身份
        $c = ''; //城市
        foreach ( $province as $value ) {
            if ( $p == '' ) {
                $p .= "'{$value['province']}':'{$value['province']}'";
            } else {
                $p .= ", '{$value['province']}':'{$value['province']}'";
            }

            $citys = $this->getModelDao()->getItems(array('province' => $value['province']));
            if ( $c == '' ) {
                $c .= "'{$value['province']}' : [";
            } else {
                $c .= ", '{$value['province']}' : [";
            }
            $cc = array();
            foreach ($citys as $city) {
                $cc[] = "{'id':'{$city['id']}', 'name':'{$city['city']}'}";
            }
            $c .= implode(", ", $cc)."]";

        }
        $buffer = "var __BANK_AREADATA__ = {\n";
        $buffer .= "\t'prov' : {";
        $buffer .= $p."}, \n";
        $buffer .= "\t'city' : {";
        $buffer .= $c."}, \n";
        $buffer .= "\t};";

        $tempContent = file_get_contents(self::JS_FILE_PATH.'BankAreaData.tpl');
        $tempContent = str_replace("{code}", $buffer, $tempContent);
        $jsfile = self::JS_FILE_PATH.'BankAreaData.js';
        return file_put_contents($jsfile, $tempContent);

    }
}
